package com.jy.appmarket.screenlock.activity;

import java.util.Locale;

import com.jy.appmarket.R;
import com.jy.appmarket.screenlock.utils.SystemSettingUtil;
import com.jy.appmarket.screenlock.utils.XiaomiUtils;
import com.rad.statistics.android.StatisticsAgent;
import com.umeng.analytics.MobclickAgent;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class LockInitSettingActivity extends Activity implements OnClickListener {

	private LinearLayout mSysLayout;

	private LinearLayout mXiaomiSetting;
	private LinearLayout mAutoStartAndTrustListSetting;
	private LinearLayout mOpenFloatingWindowsSetting;
	private static boolean isMIUI = false;
	private static String mMIUIVersion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_locker_init_setting);

		initView();
		super.onCreate(savedInstanceState);
	}

	private void initTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("初始设置");
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	private void initView() {
		initTitle();

		findViewById(R.id.setting_custom_message_layout).setOnClickListener(this);

		mSysLayout = findViewById(R.id.close_system_locker_layout);
		View line = findViewById(R.id.lock_init_setting_line1);
		mSysLayout.setOnClickListener(this);

		String str = Build.BRAND;
		if (str.toLowerCase(Locale.ENGLISH).indexOf("meizu") > -1) {
			mSysLayout.setVisibility(View.GONE);
			line.setVisibility(View.GONE);
		} else {
			mSysLayout.setVisibility(View.VISIBLE);
			line.setVisibility(View.VISIBLE);
		}

		mXiaomiSetting = findViewById(R.id.open_xiaomi_setting_layout);
		mXiaomiSetting.setOnClickListener(this);

		mOpenFloatingWindowsSetting = findViewById(R.id.open_floating_windows_setting);
		View line3 = findViewById(R.id.lock_init_setting_line3);
		mOpenFloatingWindowsSetting.setOnClickListener(this);

		mAutoStartAndTrustListSetting = findViewById(R.id.setting_auto_start_and_trustlist);
		View line4 = findViewById(R.id.lock_init_setting_line4);
		mAutoStartAndTrustListSetting.setOnClickListener(this);

		mMIUIVersion = XiaomiUtils.getSystemProperty();
		isMIUI = XiaomiUtils.isMIUI(this);

		if (isMIUI) {
			line3.setVisibility(View.VISIBLE);
			mOpenFloatingWindowsSetting.setVisibility(View.VISIBLE);
			line4.setVisibility(View.VISIBLE);
			mAutoStartAndTrustListSetting.setVisibility(View.VISIBLE);
		} else {
			line3.setVisibility(View.GONE);
			mOpenFloatingWindowsSetting.setVisibility(View.GONE);
			line4.setVisibility(View.GONE);
			mAutoStartAndTrustListSetting.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.close_system_locker_layout:// 关闭系统锁屏
			// miui系统进入开发者选项，否则进入解锁方式选择界面
			if (XiaomiUtils.isMIUI(this)) {
				XiaomiUtils.closeSystemLocker(this, true);
				if (mMIUIVersion.equals("V5")) {
					startDialog(3);
				} else if (mMIUIVersion.equals("V6")) {
					startSingleChoiceDialogActivity(getString(R.string.close_system_locker),
							getString(R.string.developer_options_up_dialog),
							getString(R.string.developer_options_dialog));
				} else if (mMIUIVersion.equals("V7")) {
					startSingleChoiceDialogActivity(getString(R.string.close_system_locker),
							getString(R.string.developer_options_up_dialog),
							getString(R.string.developer_options_dialog));
				}
			} else {
				Intent intent = new Intent("/");
				ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.ChooseLockGeneric");
				intent.setComponent(cm);
				startActivityForResult(intent, 0);
				SingleChoiceDialogActivity.startSingleChoiceDialogActivity(this,
						getString(R.string.close_system_locker), getString(R.string.choose_unlock), null);
			}

			StatisticsAgent.onEvent(this, "close_system_lock_event");

			break;

		case R.id.open_xiaomi_setting_layout:
			startActivity(new Intent(this, SpecialSettingActivity.class));

			break;

		case R.id.setting_custom_message_layout:
			try {
				if (Build.VERSION.SDK_INT < 18) {
					if (SystemSettingUtil.isAccessibilitySettingsOn(getApplicationContext())) {
						startActivity(new Intent(this, CustomMessageActivity.class));
					} else {
						startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
						startTextAndImageDialogActivity("开启【应用市场】服务", "严格保护您的隐私，只是为锁屏上显示通知",
								R.drawable.accessibility_tip);
					}
				} else {
					if (SystemSettingUtil.isNotificationEnabled(this)) {
						startActivity(new Intent(this, CustomMessageActivity.class));
					} else {
						startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
						startTextAndImageDialogActivity("开启【应用市场】读取通知权限", "严格保护您的隐私，只是为锁屏上显示通知",
								R.drawable.notification_tip);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.open_floating_windows_setting:
			if (isMIUI) {
				XiaomiUtils.setAllowFolatWindow(this, mMIUIVersion);
				if (mMIUIVersion.equals("V5")) {
					startDialog(1);
					// XiaomiDialogActivity.startXiaomiDialogActivity(this, 1);
				} else if (mMIUIVersion.equals("V6")) {
					startSingleChoiceDialogActivity(getString(R.string.open_floatwindow),
							getString(R.string.open_floatwindow_up_dialog),
							getString(R.string.open_floatwindow_dialog));
				} else if (mMIUIVersion.equals("V7")) {
					startSingleChoiceDialogActivity(getString(R.string.open_floatwindow),
							getString(R.string.open_floatwindow_up_dialog),
							getString(R.string.open_floatwindow_dialog));
				}
			}
			break;
		case R.id.setting_auto_start_and_trustlist:
			if (isMIUI) {
				boolean isRight = XiaomiUtils.setTrust(this, mMIUIVersion);
				if (isRight) {
					if (mMIUIVersion.equals("V5")) {
						startDialog(2);
					} else if (mMIUIVersion.equals("V6")) {
						startSingleChoiceDialogActivity(getString(R.string.open_auto_run),
								getString(R.string.open_auto_run_up_dialog), getString(R.string.open_auto_run_dialog));
					} else if (mMIUIVersion.equals("V7")) {
						startSingleChoiceDialogActivity(getString(R.string.open_auto_run),
								getString(R.string.open_auto_run_up_dialog), getString(R.string.open_auto_run_dialog));
					}
				} else {
					// 不做
					// completion();
				}
			}
			break;
		default:
			break;
		}
	}

	private void startTextAndImageDialogActivity(String title, String content, int resId) {
		Intent intent = new Intent(this, TextAndImageDialog.class);
		Bundle b = new Bundle();
		b.putInt(TextAndImageDialog.ICON, resId);
		b.putString(TextAndImageDialog.CONTENT, content);
		b.putString(TextAndImageDialog.TITLE, title);
		intent.putExtras(b);
		startActivityForResult(intent, 11);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) { // 根据状态码，处理返回结果
		case RESULT_OK:
			if (requestCode == 11) {
				if (Build.VERSION.SDK_INT < 18) {
					if (SystemSettingUtil.isAccessibilitySettingsOn(getApplicationContext())) {
						startActivity(new Intent(this, CustomMessageActivity.class));
					}
				} else {
					if (SystemSettingUtil.isNotificationEnabled(this)) {
						startActivity(new Intent(this, CustomMessageActivity.class));
					}
				}
			}
			break;
		default:
			break;
		}
	}

	private void startDialog(int type) {
		Intent intent = new Intent(this, XiaomiDialogActivity.class);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle b = new Bundle();
		b.putInt("miuiType", type);
		intent.putExtras(b);
		startActivityForResult(intent, 0);
	}

	private void startSingleChoiceDialogActivity(String title, String contentUp, String content) {
		Intent intent = new Intent(this, SingleChoiceDialogActivity.class);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle b = new Bundle();
		b.putString(SingleChoiceDialogActivity.CONTENT_UP, contentUp);
		b.putString(SingleChoiceDialogActivity.CONTENT, content);
		b.putString(SingleChoiceDialogActivity.TITLE, title);
		intent.putExtras(b);
		startActivityForResult(intent, 0);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
